package main

import (
	"context"
	"fmt"
	"time"
)

func run3(parentCtx context.Context) {
	ctx, cancel := context.WithCancel(parentCtx)
	go func() {
		time.Sleep(3 * time.Second)
		fmt.Println("cancel children context")
		cancel()
	}()
	select {
	case <-time.After(10 * time.Second):
		fmt.Println("oversleep")
	case <-ctx.Done():
		fmt.Println("error2: ", ctx.Err())
	}
}

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	go run3(ctx)
	time.Sleep(2 * time.Second)
	fmt.Println("cancel parent context")
	cancel()
	time.Sleep(2 * time.Second)

}

// 父context的cancel会影响子contentx的cancel
